/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.oea.hibernate.entities;

import com.oea.hibernate.utilities.HibernateUtil;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Sony
 */

@Table(name = "faculties")
@Entity
public class Faculty extends AbstractEntity
{
    @Id
    @GeneratedValue
    @Column(name = "id")
    protected  long id;
    
    @Column(name = "fname")
    protected  String fname ;
    
    @Column(name = "lname")
    protected  String lname;
    
    @Column(name = "mname")
    protected  String mname;
    
    @Column(name = "gender")
    protected  String gender;
    
    @Column(name = "email")
    protected  String email;
    
    @Column(name = "password")
    protected  String password;
    
   
   
    
    @Column(name = "contact")
    protected  String contact;
     
    
    @Column(name = "addedby")
    protected  Long addedby;

    @Column(name = "status")
    protected boolean  status;
    
    @Transient
    protected  Admin admin;

  
    
    

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }
    
    public String getFname() {
        return fname;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public String getLname() {
        return lname;
    }

    public void setLname(String lname) {
        this.lname = lname;
    }

    public String getMname() {
        return mname;
    }

    public void setMname(String mname) {
        this.mname = mname;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

   
   
    public String getContact() {
        return contact;
    }

    public void setContact(String contact) {
        this.contact = contact;
    }

   

    
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Long getAddedby() {
        return addedby;
    }

    public void setAddedby(Long addedby) {
        this.addedby = addedby;
    }
    
      public Admin getAdmin() {
        return admin;
    }

    public void setAdmin() {
        this.admin = (Admin) new Admin().getById(addedby);
    }
    
     public Faculty verify()
   {
       Faculty f=null;
       try 
       {
           Session ss=HibernateUtil.getSessionFactory().openSession();
           Query q=ss.createQuery("from Faculty where email='"+this.email+"' and password='"+this.password+"'");
           
           List<Faculty> li=q.list();
           
           if(li.size()==1)
           {
               f=li.get(0);
           }
                      
       }
       catch (Exception e) 
       {
           e.printStackTrace();
       }
       
       return f;
   }
     
     public static ArrayList<Faculty> pageFaculty(List<Faculty> li)
      {
          ArrayList<Faculty> li_st=new ArrayList<Faculty>();
          
         //  ArrayList<Student> filtered_list = new ArrayList<>();
                int end = 5;
                if (li.size() < 5) {
                    end = li.size();
                }
                for (int i = 0; i < end; i++) {
                    System.out.println(li.get(0));
                  //  li_st.add(li.get(i));
                    li_st.add(li.get(i));
                }
          
          return li_st;
      }

     public boolean facultyupdate() {
        boolean b = false;
        try {
            Session s = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = s.beginTransaction();
            //  System.out.println("parth");
            Query q = s.createQuery("update Faculty set status=" + this.status + " , addedby="+this.addedby+" where id=" + this.id + "");
            //      System.out.println("arpan");
//            q.setParameter("statuss", Boolean.TRUE);
//            q.setParameter("id", stud_id);
            q.executeUpdate();
         //   System.out.println("keval");

            //   System.out.println("bjghjbjhb");
            tx.commit();
            b = true;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return b;
    }
}
